#
# libstmconn
# Copyright 2025 STMROS Authors, All rights reserved.
#
# This module is adapted from the catkin project.
# Original copyright notice:
#
# Software License Agreement (BSD License)
#
# Copyright (c) 2012, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#    * Neither the name of the copyright holder nor the names of its
#      contributors may be used to endorse or promote products derived from
#      this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

find_package(Python3 COMPONENTS Interpreter)

function(em_expand context_in context_out em_file_in file_out)  
  # 确保输入文件存在
  if(NOT EXISTS "${context_in}")
    message(FATAL_ERROR "Input context file missing: ${context_in}")
  endif()
  if(NOT EXISTS "${em_file_in}")
    message(FATAL_ERROR "Template file missing: ${em_file_in}")
  endif()
  
  # 生成上下文文件
  message(DEBUG "Configuring file: ${context_in} -> ${context_out}")
  configure_file(${context_in} ${context_out} @ONLY)
  
  # 确保上下文文件生成成功
  if(NOT EXISTS "${context_out}")
    message(FATAL_ERROR "Context file was not generated correctly: ${context_out}")
  endif()

  # 创建输出目录（如果不存在）
  get_filename_component(_folder_out ${file_out} PATH)
  if(NOT IS_DIRECTORY ${_folder_out})
    file(MAKE_DIRECTORY ${_folder_out})
  endif()

  message(DEBUG "Evaluate template '${em_file_in}' to '${file_out}' (with context from '${context_out}')")
  execute_process(COMMAND
    ${Python3_EXECUTABLE}
    -m em
    --raw-errors
    -F ${context_out}
    -o ${file_out}
    ${em_file_in}
    RESULT_VARIABLE _res)

  if(NOT _res EQUAL 0)
    message(FATAL_ERROR "em.py return error code ${_res}")
  endif()
endfunction()